chore: stop unit test OOM from a spinning synchronizer loop - #390
Merged
Conversation
recoveryResetsToFirstAvailableSynchronizer handed the same mock synchronizer back from its factory on every build, but SourceManager closes the synchronizer it switches away from, and a closed one reports SHUTDOWN immediately. Once both mocks were closed the round-robin loop in runSynchronizers re-entered with no delay, logging on each pass into a LogCapture that retains every message, so the live heap climbed until it exhausted the test JVM. Building a fresh synchronizer per call, as the real factories do, keeps the live set flat. Also size the forked test JVM, which Gradle otherwise leaves at 512m (org.gradle.jvmargs only sizes the daemon). The suite ran against that ceiling, and the extra headroom cuts full GCs during the run from ~1050 to ~240. Co-authored-by: Cursor <cursoragent@cursor.com>
Keeps this branch dedicated to the unit test memory problem. The new Integer/new Long warnings in FlagTest are unrelated noise and belong on their own branch. Co-authored-by: Cursor <cursoragent@cursor.com>
Leaves this branch as only the fix for the spinning synchronizer loop. The heap size is headroom, not the fix, and is easier to review alone. Co-authored-by: Cursor <cursoragent@cursor.com>
This was referenced Aug 17, 2026
abelonogov-ld
enabled auto-merge
August 17, 2026 23:08
tanderson-ld
approved these changes
Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happened
FDv2DataSourceTest.recoveryResetsToFirstAvailableSynchronizerintermittently fails CI withOutOfMemoryError, reported againsttearDown'sexecutor.shutdownNow()andArrays.copyOf. It is unrelated to whatever PR happens to be running; it is a pre-existing flake in this test.Root cause
The test's synchronizer factories return the same
MockQueuedSynchronizerinstance on everybuild()call, while the real factories construct a new one.SourceManager.getNextAvailableSynchronizerAndSetActive()closes the synchronizer it is switching away from, andMockQueuedSynchronizer.next()on a closed instance returns an already-completedSHUTDOWN.So once recovery has cycled through both slots, every instance is closed and the inner loop in
runSynchronizersexits immediately, while the outer loop is round-robin (getNextAvailableSynchronizerwraps its index) and re-enters with no delay. Each pass logsSynchronizer '{}' is starting.intoLogCaptureRule'sLogCapture, which retains every message for the life of the test, so the live heap grows until the JVM is exhausted.Instrumenting the factories shows the scale of the spin. In a single ~10 second run of this one test, each factory was invoked about 630,000 times, where the test intends roughly two:
And live heap after each full GC climbs monotonically, never recovering:
The fix
Build a fresh synchronizer per factory call, as production does. A new instance is never pre-closed, so
next()serves its queued results and then returns a future that only completes later, which restores the pacing the test intended: the cycle waits on the 1s fallback and 2s recovery timers instead of spinning. Live heap after full GC then stays flat at ~75M.Why the stack trace points at
shutdownNow()That is where the OOM surfaces, not where the memory went.
shutdownNow()callsdrainQueue(), whosetoArrayneeds a fresh allocation (Arrays.copyOf), and on an already-exhausted heap that is simply the allocation that happens to fail. As the probe above shows, the executor's queue is empty (executorQueue=0): during the spin every result isSHUTDOWN, so the fallback timer never arms, and the recovery timer is not built once the index has been reset to the prime synchronizer. The retained memory is the accumulated log capture, not scheduled tasks or pending futures.Test plan
./gradlew testpasses locallyNothing shipped changes, so this is
choreand should not cut a release.Related
Raising the forked test JVM's heap is split out into its own PR as headroom. It does not fix this OOM (the retention fills any heap — it still OOMs at 2g), so this PR stands alone.
Follow-up worth considering (not in this PR)
The hot loop is reachable in principle outside tests: if every synchronizer reports
SHUTDOWNimmediately,runSynchronizersspins round-robin with no backoff, logging each pass. On a device that means CPU burn and log spam. Production factories build fresh synchronizers that block on I/O, so this is unlikely in practice, but a guard or backoff in that loop may be worth adding by whoever owns FDv2.